package com.zja.www.dao.impl;

import com.zja.www.dao.DepartmentDao;
import com.zja.www.entity.Department;
import com.zja.www.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DepartmentDaoImpl implements DepartmentDao {
    private final Connection connection;

    public DepartmentDaoImpl() {
        this.connection = DBUtil.getConnection();
        if (this.connection == null) {
            throw new RuntimeException("无法获取数据库连接");
        }

    }

    /**
     * 添加科室信息
     *
     * @param department 科室对象
     * @return 是否添加成功
     */
    @Override
    public boolean insert(Department department) {
        String sql = "insert into department(name, description) values(?,?)";
        try (PreparedStatement ps = connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS)) {
            ps.setString(1, department.getName());
            ps.setString(2, department.getDescription());

            int affectedRows = ps.executeUpdate();

            if (affectedRows > 0) {
                try (ResultSet rs = ps.getGeneratedKeys()) {
                    if (rs.next()) {
                        department.setId(rs.getInt(1));
                    }
                }
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean deleteById(int id) {
        String sql = "delete from department where id = ?";
        try (PreparedStatement ps = connection.prepareStatement(sql)) {

            ps.setInt(1, id);

            int affectedRows = ps.executeUpdate();
            return affectedRows > 0;

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean update(Department department) {
        String sql = "update department set name = ?, description = ? where id = ?";

        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setString(1, department.getName());
            ps.setString(2, department.getDescription());
            ps.setInt(3, department.getId());

            return ps.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Department selectById(int id) {
        String sql = "select * from department where id = ?";

        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setInt(1, id);

            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    Department department = new Department();
                    department.setId(rs.getInt("id"));
                    department.setName(rs.getString("name"));
                    department.setDescription(rs.getString("description"));
                    return department;
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Department> selectAll() {
        List<Department> departments = new ArrayList<Department>();

        String sql = "select * from department";
        try (Statement stmt = connection.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            while (rs.next()) {
                Department department = new Department();
                department.setId(rs.getInt("id"));
                department.setName(rs.getString("name"));
                department.setDescription(rs.getString("description"));
                departments.add(department);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return departments;
    }

    @Override
    public List<Department> selectByName(String name) {
        List<Department> departments = new ArrayList<>();

        String sql = "select * from department where name like ?";

        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setString(1, "%" + name + "%");
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    Department department = new Department();
                    department.setId(rs.getInt("id"));
                    department.setName(rs.getString("name"));
                    department.setDescription(rs.getString("description"));
                    departments.add(department);
                }
            }

        }catch(SQLException e) {
            e.printStackTrace();
        }
        return departments;
    }
}
